package com.yipingnai.blog.controller.admin;

import com.yipingnai.blog.model.RespBean;
import com.yipingnai.blog.model.Tags;
import com.yipingnai.blog.service.TagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: jiangym
 * @Date: 2020/5/19 11:05
 */
@RestController
@RequestMapping("/admin/tags")
public class AdminTagsController {

    @Autowired
    TagsService tagsService;

    @GetMapping("/")
    public List<Tags> getAllTags(){
        return tagsService.getAllTags();
    }

    @DeleteMapping("/")
    public RespBean deleteTagById(Integer id){
        if(tagsService.deleteTagById(id)){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PostMapping("/")
    public RespBean addTag(String tagName){
        Integer result = tagsService.addTag(tagName);
        if( result > 0){
            return RespBean.ok("添加成功");
        }else if(result == -1){
            return RespBean.error("该标签已存在");
        }
        return RespBean.error("添加失败");
    }
}
